package ar.org.cfired.creditos.abm;

import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
import ar.org.cfired.hibernate.utils.HibernateUtil;
import ar.org.cfired.creditos.abm.Destinatario;
import ar.org.cfired.creditos.abm.exception.DestinatarioException;

public class AbmDestinatario {
private Session session;
private ArrayList<Destinatario> arrDestinatario;
private Transaction  t;
	
	public AbmDestinatario (Session s){
		session = s;	
		if(session.getTransaction() == null)
			this.t = session.beginTransaction();
		
	}
	
	public AbmDestinatario(){
		
	}
	
	public AbmDestinatario(ArrayList<Destinatario> d){
		this.arrDestinatario = d;
		if(session == null){
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			this.t = session.beginTransaction();
		}
	}
	public AbmDestinatario(ArrayList<Destinatario> d, Session s){
		try{
			System.out.println(d.get(0).getObsUsuario());
			this.arrDestinatario = d;
			this.session = s;	
			if(session.getTransaction() == null)
				t = session.beginTransaction();
		}
		catch(HibernateException e){
			DestinatarioException ex = new DestinatarioException();
			
		}
	}
	
	public void setGuardar(){
		try{
			if(!arrDestinatario.isEmpty()){
				for(int i = 0; i< arrDestinatario.size(); i++){
					System.out.println("Imprimo el OBS ID  antes de guardar : " + this.arrDestinatario.get(i).getDesId());
					session.save(arrDestinatario.get(i));
				}
			}
		}
		catch(HibernateException e){
			//exception propia
			t.rollback();			
		}
		
		finally{
			
			if(t != null)
				if(t.isActive()){
					t.commit();
					}
				else
					t.rollback();		
		}
					
					
		
	}
	
	public void setEliminar(){
		try{
			if(session == null)
				session = HibernateUtil.getSessionFactory().getCurrentSession();
			if(session.getTransaction() == null)
				t = session.beginTransaction();
			if(!arrDestinatario.isEmpty()){
				for(int i = 0; i<= arrDestinatario.size(); i++)
					session.delete(arrDestinatario.get(i));
			}
		}
		catch(HibernateException e){
			//exception propia
			t.rollback();			
		}
		
		finally{
			if(t != null)
				if(t.isActive()){
					t.commit();
					}
				else
					t.rollback();
			else
				t.rollback();
				}
					
	}
	
	public Iterator<Destinatario> getBuscarDestinatario(){
		//System.out.println("El id vale " + this.arrDestinatario.get(0).getDesId());
		List<Destinatario> respuesta;  
		respuesta = (List<Destinatario>) session.createQuery("from Destinatario where des_id=" + this.arrDestinatario.get(0).getDesId()).list();
		//Query q = session.getNamedQuery("select_Destinatarios"); //.setResultTransformer(Transformers.aliasToBean(Observaciones.class));
		//q.setString("id", "1");
		//respuesta = q.list();
		Iterator<Destinatario> iter = respuesta.iterator();
		return (iter);
		
	}
	
	public void addDestinatario(ArrayList<Destinatario> d){
		this.arrDestinatario = d;		
	}
	
}
